package com.baiye.template.service.impl;

import com.baiye.template.dao.PermissionDao;
import com.baiye.template.pojo.Permission;
import com.baiye.template.pojo.Role;
import com.baiye.template.service.PermissionService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/9/7 16:46
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public int deleteById(int id) {
        return permissionDao.deleteById(id);
    }

    @Override
    public int addPermission(Permission permission) {
        return permissionDao.add(permission);
    }

    @Override
    public int addPermissionToRole(int roleId, int permissionId) {
        return permissionDao.addPermissionToRole(roleId,permissionId);
    }

    @Override
    public int deletePermissionToRole(int roleId, int permissionId) {
        return permissionDao.deletePermissionToRole(roleId, permissionId);
    }

    @Override
    public List<Permission> getPermission(Permission permission, Integer pageIndex,Integer pageSize) {
        int start = (pageIndex-1)*pageSize;
        PageHelper.startPage(pageIndex,pageSize);
        return permissionDao.getPermission(permission, start,pageSize);
    }

    @Override
    public List<Permission> getPermissionByRoleId(Integer roleId) {
        List<Permission> permission = permissionDao.getPermissionByRoleIdExist(roleId);
        for(Permission p:permission){
            p.setOwned(true);
        }
        permission.addAll(permissionDao.getPermissionByRoleIdNotExist(roleId));
        return permission;
    }

    @Override
    public int updatePermission(Permission permission) {
        return permissionDao.updatePermission(permission);
    }

    @Override
    public List<Role> selectListByPath(String requestUrl) {
        return permissionDao.selectListByPath(requestUrl);
    }
}
